package cn.wizzer.app.sys.modules.services.impl;

import cn.wizzer.app.sys.modules.models.SysPayWay;
import cn.wizzer.app.sys.modules.models.SysPayWayBank;
import cn.wizzer.app.sys.modules.models.vo.PayWayBankVO;
import cn.wizzer.app.sys.modules.services.SysPayWayBankService;
import cn.wizzer.app.sys.modules.services.SysPayWayService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.base.service.CommonDBService;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.ArrayUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.plugins.wkcache.annotation.CacheDefaults;
import org.nutz.plugins.wkcache.annotation.CacheRemoveAll;


@IocBean(args = {"refer:dao"})
@Service(interfaceClass = SysPayWayBankService.class)
@CacheDefaults(cacheName = "sys_pay_way_bank")
public class SysPayWayBankServiceImpl extends BaseServiceImpl<SysPayWayBank> implements SysPayWayBankService {
    public SysPayWayBankServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private CommonDBService dbService;
    @Inject
    private SysPayWayService sysPayWayService;

    @Override
    public Pagination listPage(Integer pageNumber, int pageSize, String searchKeyword, String[] searchDate) {
        // 根据收款人,银行卡号搜索
        Sql sql = Sqls.create("SELECT pb.*,c.country,c.logo AS flag,b.name AS bank,b.logo,b.shortName,pw.payWay FROM sys_pay_way_bank AS pb");
        Sql countSql = Sqls.create("SELECT count(1) FROM sys_pay_way_bank AS pb");
        String joinSql = "LEFT JOIN sys_bank_lib AS b ON pb.bankLibId = b.id " +
                "LEFT JOIN sys_pay_way AS pw ON pw.id = pb.payWayId " +
                "LEFT JOIN sys_currency AS c ON b.currencyId = c.id $condition ";
        sql.appendSourceSql(joinSql);
        countSql.appendSourceSql(joinSql);

        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(searchKeyword)) {
            searchKeyword = "%" + searchKeyword + "%";
            cnd.and(Cnd.exps("b.name", "like", searchKeyword)
                    .or("b.shortName", "like", searchKeyword)
                    .or("pb.code", "like", searchKeyword)
                    .or("c.country", "like", searchKeyword));
        }
        if (ArrayUtils.isNotEmpty(searchDate)) {
            cnd.and("opAt", ">=", Times.d2TS(Times.D(searchDate[0])));
            cnd.and("opAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        sql.setCondition(cnd);
        sql.appendSourceSql("order by pb.sort asc");
        countSql.setCondition(cnd);
        return dbService.listPage(PayWayBankVO.class, pageNumber, pageSize, sql.toString(), countSql.toString());
    }

    @Override
    public void add(SysPayWayBank bank) {
        bank.setOpAt(1L);
        this.insert(bank);
        this.clearCache();
    }

    @Override
    public void edit(SysPayWayBank bank) {
        this.update(bank);
        this.clearCache();
    }

    @Override
    @CacheRemoveAll
    public void clearCache() {

    }

    @Override
    public PayWayBankVO fetchVO(String id) {
        return dbService.fetchSql(PayWayBankVO.class,
                "SELECT pb.*,c.country,c.logo AS flag,b.name AS bank,b.logo,b.currencyId,pw.payWay FROM sys_pay_way_bank AS pb " +
                        "LEFT JOIN sys_bank_lib AS b ON pb.bankLibId = b.id " +
                        "LEFT JOIN sys_pay_way AS pw ON pw.id = pb.payWayId " +
                        "LEFT JOIN sys_currency AS c ON b.currencyId = c.id " +
                        Cnd.where("pb.id", "=", id).toString()
        );
    }

    @Override
    public SysPayWayBank query(String bankLibId, String channel, String currencyId) {
        SysPayWay sysPayWay = sysPayWayService.fetch(Cnd.where("currencyId", "=", currencyId)
                .and("payWay", "=", channel));
        return this.fetch(Cnd.where("bankLibId", "=", bankLibId).and("payWayId", "=", sysPayWay.getId()));
    }

}
